package com.ruoyi.finance.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.finance.domain.vo.PaymentRequestVo;
import com.ruoyi.finance.mapper.vo.PaymentRequestVoMapper;
import com.ruoyi.finance.mapper.PaymentVoucherMapper;
import com.ruoyi.finance.mapper.vo.PaymentVoMapper;
import com.ruoyi.finance.service.IPaymentRequestService;
import com.ruoyi.finance.service.IPaymentVoucherService;
import com.ruoyi.procured.domain.PaymentRequest;
import com.ruoyi.procured.domain.PaymentRequestProcess;
import com.ruoyi.procured.domain.vo.PaymentVo;
import com.ruoyi.procured.mapper.PaymentRequestMapper;
import com.ruoyi.procured.service.IPaymentRequestProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class PaymentRequestImpl extends ServiceImpl<PaymentRequestMapper, PaymentRequest> implements IPaymentRequestService
{
    @Autowired
    private PaymentRequestVoMapper paymentRequestVOMapper;

    @Autowired
    private PaymentRequestMapper paymentRequestMapper;

    @Autowired
    private IPaymentRequestProcessService paymentRequestProcessService;

    @Autowired
    private PaymentVoucherMapper paymentVoucherMapper;

    @Autowired
    private PaymentVoMapper paymentVoMapper;

    @Autowired
    private IPaymentVoucherService paymentVoucherService;
    /**
     * 查询申请款列表
     *
     * @param paymentRequestVO
     * @return 申请款集合
     */
    @Override
    public List<PaymentRequestVo> selectPaymentRequestList(PaymentRequestVo paymentRequestVO)
    {
        return paymentRequestVOMapper.selectPaymentRequestList(paymentRequestVO);
    }

    /**
     * 驳回申请款
     */
    @Override
    @Transactional
    public void rejectPaymentRequest(List<String> paymentRequestIds, String processRemark, Long userId) {
        //校验是否存在【已驳回】
        CheckPaymentRequestStatus(paymentRequestIds,"2","已驳回","");

        //校验付款状态是否为未付款
        CheckPaymentRequestStatus(paymentRequestIds,"","非未付款","0");

        paymentRequestIds.stream()
                .forEach(paymentRequestId -> {
                    PaymentRequestProcess paymentRequestProcess = new PaymentRequestProcess();
                    paymentRequestProcess.setReviewerId(userId);
                    paymentRequestProcess.setPaymentRequestId(paymentRequestId);
                    paymentRequestProcess.setProcessStatus("2");
                    paymentRequestProcess.setProcessRemark(processRemark);
                    paymentRequestProcessService.updateProcessStatus(paymentRequestProcess);
                });
    }
    /**
     * 核准申请款
     */
    @Override
    @Transactional
    public void approvedPaymentRequest(List<String> paymentRequestIds, Long userId, String nickName) {

        //检查是否为【审核中】状态
        CheckPaymentRequestStatus(paymentRequestIds,"0","非审核中","");

        paymentRequestIds.stream()
                .forEach(paymentRequestId -> {
                    PaymentRequestProcess paymentRequestProcess = new PaymentRequestProcess();
                    paymentRequestProcess.setReviewerId(userId);
                    paymentRequestProcess.setPaymentRequestId(paymentRequestId);
                    paymentRequestProcess.setProcessStatus("1");
                    paymentRequestProcessService.updateProcessStatus(paymentRequestProcess);
                });

        //生成付款单
        paymentVoucherService.voucherPaymentRequest(paymentRequestIds,nickName);
    }


    //校验申请单状态
    // paymentReviewStatus:0审核中，1已审核，2已驳回
    // paymentStatus：0未付款
    public void CheckPaymentRequestStatus(List<String> paymentRequestIds,String paymentReviewStatus, String message,String paymentStatus){
        LambdaQueryWrapper<PaymentRequest> qw = new LambdaQueryWrapper<>();
        qw.in(PaymentRequest::getPaymentRequestId,paymentRequestIds);
        if(StringUtils.isNotEmpty(paymentReviewStatus)){
            qw.eq(PaymentRequest::getPaymentReviewStatus,paymentReviewStatus);
        }
        if(StringUtils.isNotEmpty(paymentStatus)){
            qw.eq(PaymentRequest::getPaymentStatus,paymentStatus);
        }
        List<PaymentRequest> result = paymentRequestMapper.selectList(qw);

        if (result.size() != paymentRequestIds.size()) {
            throw new ServiceException("所选记录含有【"+message+"】的数据，请刷新页面重新选择");
        }

    }

    @Override
    public List<PaymentVo> selectPaymentVotList(PaymentVo paymentVo) {

        return paymentVoMapper.selectPaymentVotList(paymentVo);
    }
}
